package cn.finder.datatunnel;

import org.apache.log4j.Logger;

import cn.finder.datatunnel.processor.Processor;
import cn.finder.datatunnel.protocol.Tcp;
import cn.finder.datatunnel.protocol.Udp;


/**
 *
 * 访问控制  代理
 */
public class DT 
{
	
	private static Logger logger=Logger.getLogger(DT.class);
	
	

	
	/***
	 * dt -start
	 * dt -stop
	 * @param args
	 * @throws Exception 
	 */
    @SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception
    {
    	//1.加载 属性文件参数
    	//2.args为最新参数
    	boolean configSuccess=DTConfig.load();
    	
    	if(!configSuccess){
    		return;
    	}
    	
    	
    	
    	
    	Class<? extends Processor> processorClass=null;
    	try {
			 processorClass=(Class<? extends Processor>) Class.forName(DTConfig.dt_client_data_processor);
			 
			 try {
				//Processor processor= (Processor)processorClass.newInstance(); 
				
				if("TCP".equalsIgnoreCase(DTConfig.dt_client_read_protocol)){
					Tcp tcp=new Tcp();
					//tcp.setProcessor((Processor)processorClass.newInstance());
					tcp.setProcessorClass(processorClass);
					tcp.start();
					
				}else if("UDP".equalsIgnoreCase(DTConfig.dt_client_read_protocol)){
					Udp udp=new Udp();
					//udp.setProcessor((Processor)processorClass.newInstance());
					udp.setProcessorClass(processorClass);  //此处这样写 是因为防止并发 处理类应该多实例
					udp.start();
				}
				
				
			} catch (InstantiationException e) {
				logger.error(e);
			} catch (IllegalAccessException e) {
				logger.error(e);
			}
			 
			 
			 
		} catch (ClassNotFoundException e1) {
			logger.error(e1);
			return;
		}
    	
    	
    	
    	 logger.debug("====socket listening port:"+DTConfig.dt_client_listen_port+" stopped");
    	
    	
    	
    }
}
